package com.example.dao;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.common.Constants;
import com.example.dao.base.BaseEntity;
import com.example.dao.entityintf.EntityInterface;
import com.example.validation.ValidEmail;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.*;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Set;

/**
 * 用户实体类，实现了 UserDetails 接口
 */
@TableName("sys_users")
public class User extends BaseEntity implements UserDetails, EntityInterface,Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 自增长 ID，唯一标识
     */
    @Setter
    @Getter
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 用户账号
     */
    @Setter
    @Getter
    @Size(min = 3, max = 20, message = "{Size.userDto.username}")
    private String username;

    /**
     * 手机号
     */
    @Setter
    @Getter
    @Pattern(regexp = Constants.PATTERN_MOBILE, message = "{Size.userDto.mobile}")
    private String mobile;

    /**
     * 姓名
     */
    @Setter
    @Getter
    @Size(min = 1, max = 20, message = "{Size.userDto.name}")
    private String name;

    /**
     * 是否激活，默认激活
     */
    @Setter
    private Boolean enabled = true;

    /**
     * 账户是否未过期，默认未过期
     */
    @Setter
    @TableField("account_non_expired")
    private Boolean accountNonExpired = true;

    /**
     * 账户是否未锁定，默认未锁定
     */
    @Setter
    @TableField("account_non_locked")
    private Boolean accountNonLocked = true;

    /**
     * 密码是否未过期，默认未过期
     */
    @Setter
    @TableField("credentials_non_expired")
    private Boolean credentialsNonExpired = true;

    /**
     * 密码哈希
     */
    @Setter
    @Getter
    @TableField("password_hash")
    private String password;

    /**
     * 电邮地址
     */
    @Setter
    @Getter
    @Size(min = 1, max = 50, message = "{Size.userDto.email}")
    @ValidEmail
    private String email;

    /**
     * 是否启用两步验证
     */
    @Setter
    @Getter
    @TableField("using_mfa")
    private boolean usingMfa = false;

    /**
     * 两步验证的key
     */
    @Setter
    @Getter
    @JsonIgnore
    @TableField("mfa_key")
    private String mfaKey;

    /**
     * 角色列表，使用 Set 确保不重复
     */
    @Setter
    @Getter
    @TableField(exist=false)
    private Set<Role> authorities;

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public boolean isUsingMfa() {
        return usingMfa;
    }
    /**
     * 前端table使用
     * @return
     */
    @Override
    public Long getKey() {
        return id;
    }
}
